<template>
    <div class="pagebody">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 小程序支付二维码 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="search-box">
                <el-form :inline="true">
                    <el-form-item label="类型">
                        <el-select v-model="searchSet.type" placeholder="选择类型" clearable>
                            <el-option v-for="(item, index) in typeDict" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-select v-model="searchSet.status" placeholder="选择类型" clearable>
                            <el-option v-for="(item, index) in statusDict" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-button type="primary" icon="el-icon-refresh" @click="handleSearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleAddNew">新建</el-button>
                </el-form>
            </div>
            <el-table :data="tableData" v-loading="tableLoading" border class="table" ref="mainTable" header-cell-class-name="table-header">
                <el-table-column label="图片" min-width="130" align="center">
                    <template slot-scope="scope">
                        <el-image
                            v-if="scope.row.url"
                            class="UserCheckImage"
                            :src="scope.row.url"
                            fit="contain"
                            :preview-src-list="[scope.row.url]"
                        ></el-image>
                        <div v-else>暂无图片</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="type"
                    label="类型"
                    align="center"
                    :formatter="
                        (r, c, v, i) => {
                            return this.typeDict[v] ? this.typeDict[v].name : '未知';
                        }
                    "
                ></el-table-column>
                <el-table-column
                    prop="status"
                    label="状态"
                    align="center"
                    :formatter="
                        (r, c, v, i) => {
                            return this.statusDict[v] ? this.statusDict[v].name : '未知';
                        }
                    "
                ></el-table-column>
                <el-table-column prop="addtime" label="添加时间" align="center"></el-table-column>

                <el-table-column label="操作" width="140" align="center">
                    <template slot-scope="scope">
                        <el-button
                            v-if="scope.row.status == 0"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleActive(scope.$index, scope.row)"
                            >激活</el-button
                        >
                        <el-button
                            v-if="scope.row.status == 1"
                            type="text"
                            icon="el-icon-delete"
                            class="color-red"
                            @click="handleDelete(scope.$index, scope.row)"
                            >暂停</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <el-dialog title="提交图片" v-dialogDrag center :visible.sync="editVisible" width="60%" v-if="editVisible">
            <el-form ref="form" :model="form" :rules="formRules" label-width="120px">
                <el-form-item label="类型" prop="type">
                    <el-select v-model="form.type" placeholder="选择类型" clearable class="formInput">
                        <el-option v-for="(item, index) in typeDict" :key="index" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="图片" prop="imageid">
                    <customUpload
                        accept="image/*"
                        ref="UploadImage"
                        :limit="1"
                        listType="picture"
                        tip="只能上传jpg/png文件"
                        v-on:update:idlist="UpdateImageList"
                    >
                    </customUpload>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import API from '../../../api/index'; //读入restfulAPI
import StringTool from '../../../utils/StringTool';
import MainTool from '../../../utils/MainTool';
import JSTool from '../../../utils/JSTool';
import dataTable from './dataTable';
import customUpload from '../../common/upload/customUpload.vue';
export default {
    name: 'nationTable',
    mixins: [dataTable],
    components: {
        customUpload
    },
    data() {
        return {
            searchSet: {
                type: '',
                status: ''
            },
            query: {},
            editVisible: false,
            editIsNewAdd: false,
            form: {
                imageid: null,
                type: null
            },
            formRules: {
                type: [{ required: true, message: '参数不能为空', trigger: 'change' }],
                imageid: [{ required: true, message: '参数不能为空', trigger: 'change' }]
            },
            typeDict: { 1: { id: 1, name: '微信' }, 2: { id: 2, name: '支付宝' } },
            statusDict: { 1: { id: 1, name: '激活' }, 0: { id: 0, name: '暂停' } }
        };
    },
    created() {
        this.init();
    },
    mounted() {
        // this.PreviewPhotoList = ["http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]
        // this.$refs.PreviewImage.clickHandler()
    },
    methods: {
        //页面初始化
        init() {
            this.getData();
        },

        getData() {
            this.tableLoading = true;
            API.StandardDoWith(API.Userchargeqrcodeimage.Get(this.getQuery()), this, {
                success: (detail) => {
                    this.itemTotal = detail.count;
                    detail.list.map((it) => {
                        it.url = MainTool.FileIdToUrl(it.imageid);
                    });
                    this.tableData = detail.list;
                },
                complete: () => {
                    this.tableLoading = false;
                }
            });
        },

        handleAddNew() {
            this.editVisible = true;
            this.form = {
                imageid: null,
                type: null
            };
        },
        UpdateImageList(idlist) {
            if (idlist && idlist.length > 0) {
                this.form.imageid = idlist[0];
            } else {
                this.form.imageid = null;
            }
        },
        // 编辑操作
        handleActive(index, row) {
            this.$confirm('确定要激活吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    API.StandardDoWith(API.Userchargeqrcodeimage.Put({ params: { id: row.id } }), this, {
                        success: (detail) => {
                            this.$message.success(`成功激活`);
                            this.getData();
                        }
                    });
                })
                .catch(() => {});
        },

        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要暂停吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    API.StandardDoWith(API.Userchargeqrcodeimage.Delete({ id: row.id }), this, {
                        success: (detail) => {
                            this.$message.success(`成功暂停`);
                            this.getData();
                        }
                    });
                })
                .catch(() => {});
        },
        saveEdit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    let net = API.Userchargeqrcodeimage.Post(this.form);
                    API.StandardDoWith(net, this, {
                        success: (detail) => {
                            this.editVisible = false;
                            this.$message.success(`成功提交`);
                            this.getData();
                        }
                    });
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
@import '../../../assets/css/dataTable.css';
.UserCheckImage {
    max-height: 100px;
    max-width: 100px;
    width: 100%;
}
</style>
